<template>
  <view>
    <view class="goods-list">
      <!-- 商品列表项-->
      <view class="goods-list-item" @click="$emit('goGoodsDetial')">
        <!-- 左侧的图片区域 -->
        <view class="goods-list-left">
          <!-- 增加radio选中的组件 -->
          <radio :checked="item.goods_state" color="#C00000" v-if="showRadio" @click="radioClickHandler"></radio>
          <image :src="item.goods_small_logo ||defaultPic" mode="" class="img"></image>
        </view>
        <!-- 右边的商品详情部分 -->
        <view class="goods-list-right">
          <!-- 商品标题区域 -->
          <view class="goods-list-title">{{item.goods_name}}</view>
          <view class="goods-info-box">
            <!-- 商品价格区域 -->
            <view class="goods-list-price">¥{{item.goods_price | tofixed}}</view>
            <!-- 商品的数量区域 -->
            <uni-number-box :min="1" :value="item.goods_count" v-if="showNum" @change="numChangeHandler"></uni-number-box>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 默认的空图片
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    filters: {
      // 定义处理价格的过滤器
      tofixed(value) {
        return Number(value).toFixed(2)
      }
    },
    props: {
      item: {
        type: Object,
        default: {}
      },
      // 控制radio组件的显示与隐藏  只有在购物车页面让显示这个单选框
      showRadio: {
        type: Boolean,
        default: false
      },
      // 控制Number组件的显示与隐藏的
      showNum: {
        type: Boolean,
        default: false
      }
    },
    methods: {
      radioClickHandler() {
        this.$emit("radio-change", {
          // 商品的 Id
          goods_id: this.item.goods_id,
          // 商品最新的勾选状态 这里要取反
          goods_state: !this.item.goods_state
        })
      },
      numChangeHandler(value) {
        console.log(value)
        this.$emit("num-change", {
          // 商品的 Id
          goods_id: this.item.goods_id,
          // 商品的最新数量
          goods_count: +value //更新后的商品数量
        })
      }
    }
  }
</script>

<style lang="scss">
  .goods-list-item {
    // 让 goods-item 项占满整个屏幕的宽度
    width: 750rpx;
    // 设置盒模型为 border-box
    box-sizing: border-box;
    display: flex;
    padding: 10px 5px;
    border-bottom: 1px solid #f0f0f0;

    .goods-list-left {
      margin-right: 5px;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .img {
        width: 100px;
        height: 100px;
        display: block;
      }
    }

    .goods-list-right {
      display: flex;
      // flex: 1;
      flex-direction: column;
      justify-content: space-between;

      .goods-list-title {
        font-size: 13px;
      }

      .goods-list-price {
        font-size: 16px;
        color: #c00000;
      }
    }
  }

  .goods-info-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
</style>
